SRCURL = "http://downloads.sourceforge.net/project/boost/boost/%(DOTVERSION)s/boost_%(VERSION)s.%(EXTENSION)s"
DEFAULT_VERSION = "1_55_0"
VERSIONS = ["1_35_0", "1_42_0", "1_47_0", "1_49_0", "1_55_0"]
DEPENDS = ["zlib", "bzip2", "python"]
TODO = "test Linux version, bzip2 and zlib depends not worked out" 

def processurl(machine, dirs, version):
  return SRCURL % { "DOTVERSION" : version.replace('_','.'), "VERSION" : version, "EXTENSION" : "zip" if machine.isWindows() else "tar.gz" }

URL = processurl

from source.builders import *

class BoostBuilder( BjamBuilder ):
  def buildbjam( self, bjambuilddir ):
    bootstrapcommand = self.extractedpath + "/bootstrap.sh"
    if os.path.isfile(bootstrapcommand):
      os.chdir( self.extractedpath )
      self.execute(bootstrapcommand)
      self.bjamexecutable = self.extractedpath + "/bjam"
    else:
      os.chdir( bjambuilddir )
      self.execute( "./build.sh" )
      bjamdir = [x for x in os.listdir(".") if x.startswith("bin.")][0]
      self.bjamexecutable = bjambuilddir + "/" + bjamdir +"/bjam"

  def configure( self ):
    bjambuilddir = os.path.join(self.extractedpath, "tools","jam","src")
    self.buildbjam( bjambuilddir )
    os.chdir( self.extractedpath )

    #set environment variables for iostream
    os.environ['BZIP2_INCLUDE'] = self.dependencies['bzip2'].targetpath()+os.sep+"include"
    os.environ['BZIP2_LIBPATH'] = self.dependencies['bzip2'].targetpath()+os.sep+"lib"
    os.environ['ZLIB_INCLUDE'] = self.dependencies['zlib'].targetpath()+os.sep+"include"
    os.environ['ZLIB_LIBPATH'] = self.dependencies['zlib'].targetpath()+os.sep+"lib"

    # Generate the project-config.jam file
    f = file("tools/build/v2/user-config.jam", "w")
    pythonbindir = self.dependencies["python"].getbindir()
    pythonlibdir = self.dependencies["python"].getlibdir() + ("s", "" )[self.machine.isLinux()]
    pythonversion = self.dependencies["python"].libversion()
    pythonincludedir = self.dependencies["python"].getincludedir()

    f.write("using python : "+ pythonversion +" : "+ pythonbindir + "/python : " + pythonincludedir + " : " + pythonlibdir + " : <python-debugging>off ;\n" +
            "using python : "+ pythonversion +" : "+ pythonbindir + "/python : " + pythonincludedir + " : " + pythonlibdir + " : <python-debugging>on ;\n")
    #f.write("using python : : "+ pydir + "/bin/python ;\n")
    f.close()

  def gettoolset( self ):
    return "gcc"
    #return "pgi"
 
  def makeparameters( self ):
    if self.runtime.build_linktype == "shared":
      return ["toolset=" + self.gettoolset(), "link=shared", "runtime-link=shared", "stage", "release"]
    else:
      return ["toolset=" + self.gettoolset(), "link=static", "runtime-link=static", "stage", "release"]

 
  def installparameters( self ):
    if self.runtime.build_linktype == "shared":
      return ["--prefix="+self.installpath(), "toolset=" + self.gettoolset(), "link=shared", "runtime-link=shared"]
    else:
      return ["--prefix="+self.installpath(), "toolset=" + self.gettoolset(), "link=static", "runtime-link=static"]


class BoostBuilderWindows( BoostBuilder ):
  def buildbjam( self, bjambuilddir ):
    bootstrapcommand = self.extractedpath + "/bootstrap.bat"
    if os.path.isfile(bootstrapcommand):
      os.chdir( self.extractedpath )
      self.execute(bootstrapcommand)
      self.bjamexecutable = self.extractedpath + "/bjam.exe"
    else:
      os.chdir( bjambuilddir ) 
      bjamdir = "bin."
      self.execute( "build.bat" )
      bjamdir += "ntx86"
  
      if self.machine.bits == 64:
        bjamdir += "_64"
  
      self.bjamexecutable = bjambuilddir + "/" + bjamdir +"/bjam.exe"

  def configure( self ):
    bjambuilddir = self.extractedpath + "/tools/jam/src"
    self.buildbjam( bjambuilddir )

    # Generate the project-config.jam file
    os.chdir( self.extractedpath )
    f = file("tools/build/v2/user-config.jam", "w")

    pythonmod = self.dependencies["python"]
    pydir = pythonmod.targetpath().replace("\\", "\\\\")

    libversion = pythonmod.libversion() 

    pythonname = "python"+libversion
    f.write("using python : "+libversion+" : "+ pydir + "\\\\bin\\\\python : " +
            pydir + "\\\\include : " + pydir + "\\\\libs : <python-debugging>off ;\n" +
            "using python : "+libversion+" : "+ pydir + "\\\\bin\\\\python_d : " + 
            pydir + "\\\\include\\\\"+pythonname+" : " + pydir + "\\\\libs : <python-debugging>on ;\n")
    f.close()


  def gettoolset( self ):
    return "msvc"
 
  def makeparameters( self ):
    if self.runtime.build_linktype == "shared":
      makeparams = ["--toolset=msvc",  "link=shared", "runtime-link=shared", "architecture=x86"]
    else:
      makeparams = ["--toolset=msvc",  "link=static", "runtime-link=static", "architecture=x86"]

    #makeparams.append( "define=_BIND_TO_CURRENT_VCLIBS_VERSION" )
    if self.machine.bits == 64:
      makeparams.append( "address-model=64" )
    makeparams.append( "stage" )
    makeparams.append( ("release","debug")[self.debug] )
    return makeparams


  def installparameters( self ):
    return ["--prefix="+self.installpath()] + self.makeparameters()


def decider( machine, dirs ):
  if machine.isWindows():
    return BoostBuilderWindows
  else:
    return BoostBuilder

BUILDER = decider
